﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostSharp.Aspects;
using System.Diagnostics;

namespace Zadatak1.PostSharp
{
    [Serializable]
    class TimerAspect : OnMethodBoundaryAspect
    {
        [NonSerialized]
        private Stopwatch stopwatch = new Stopwatch();

        public override void RuntimeInitialize(System.Reflection.MethodBase method)
        {
            stopwatch = new Stopwatch();
        }

        public override void OnEntry(MethodExecutionArgs args)
        {
            stopwatch.Start();
        }

        public override void OnSuccess(MethodExecutionArgs args)
        {
            stopwatch.Stop();
            Console.WriteLine("Method {0}: duration={1}", args.Method.Name, stopwatch.Elapsed);
        }
    }

    public class FibonacciGenerator
    {
        [TimerAspect]
        public int generate(int n)
        {
            return (n == 0 || n == 1) ? n : generate(n - 1) + generate(n - 2);
        }
    }

    class ProgramPostSharp
    {
        public static void Run()
        {
            Console.WriteLine("Fib(10)={0}", new FibonacciGenerator().generate(10));
        }
    }
}
